package com.itheima.tlias84.aspect;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tlias84.utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 统计接口耗时切面
 */
@Aspect
@Component
@Slf4j
public class CountTimeAspect {
    @Resource
    HttpServletRequest request;

    @Around("execution(* com.itheima.tlias84.service.impl.*.*(..))")
    public Object count(ProceedingJoinPoint point) {

        String token = request.getHeader("token");
        log.info("获取token{}", token);
        if (Objects.nonNull(token)) {
            JSONObject jsonObject = JwtUtil.verifyToken(token);
        }
        //获取参数
        Object[] args = point.getArgs();
        log.info("请求参数{}", args);
        //获取类名
        Object target = point.getTarget();
        log.info("获取类名{}", target);
        long start = System.currentTimeMillis();
        //执行父类方法
        try {
            Object proceed = point.proceed();
            long end = System.currentTimeMillis() - start;
            log.info("{}的执行时间为{}", point.getSignature().getName(), end);
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException();
        }

    }
}
